package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.common.StrToCompanys;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.PcbaCompanyDao;
import com.sailfish.springbootdemo.dao.db1.PcbaProcessDao;
import com.sailfish.springbootdemo.dao.db1.PcbaWorkshopDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.PcbaCompany;
import com.sailfish.springbootdemo.pojo.db1.PcbaWorkshop;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PcbaCompanyServiceImpl implements PcbaCompanyService {

    @Autowired
    private PcbaCompanyDao pcbaCompanyDao;

    @Autowired
    private PcbaWorkshopDao pcbaWorkshopDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private PcbaProcessDao pcbaProcessDao;

    @Qualifier("MysqlTransactionManager1")
    @Autowired
    private DataSourceTransactionManager transactionManager1;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "工厂管理";

    @Autowired
    private StrToCompanys strToCompanys;

    @Override
    public Result getAllCompany(String searchValue) {
        try {
            //String isInner = UserHeaderHolder.getIsInner();
            //String belongPcba = UserHeaderHolder.getBelongPcba();
            /*if("2".equals(isInner) && "".equals(belongPcba)) {
                return ResultUtil.error(500, "未设置用户所属CM厂", "Error Set User", null);
            }*/
            List<PcbaCompany> list = strToCompanys.getPcbaCompanyListFromStr();

            for (PcbaCompany pcbaCompany : list) {
                List<PcbaWorkshop> pcbaWorkshopList = pcbaWorkshopDao.getAllPcbaWorkshopByCompanyId("", pcbaCompany.getPcbaCompanyId());
                pcbaCompany.setPcbaWorkshopList(pcbaWorkshopList);
            }
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Transactional(transactionManager = "MysqlTransactionManager1")
    @Override
    public Result deleteCompanyAndWorkshop(Integer pcbaCompanyId) {
        try {
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            PcbaCompany oldPcbaCompany = pcbaCompanyDao.getPcbaCompanyById(pcbaCompanyId);
            /*pcbaCompanyDao.deletePcbaCompany(pcbaCompanyId);
            pcbaWorkshopDao.deleteWorkshopByCompanyId(pcbaCompanyId);*/
            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeletePcbaCompany(pcbaCompanyId);
            if (stringIntegerMap.get("pcbaLineNum") > 0 || stringIntegerMap.get("pcbaWorkshopNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("pcbaLineNum") + "处关联线体，" +
                                stringIntegerMap.get("pcbaWorkshopNum") + "处关联车间"
                        ,
                        "Forbidden Delete", null);
            }

            Integer integer = pcbaCompanyDao.deletePcbaCompany(pcbaCompanyId);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldPcbaCompany), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public PcbaCompany getPcbaCompanyById(Integer pcbaCompanyId) {
        return pcbaCompanyDao.getPcbaCompanyById(pcbaCompanyId);
    }

    @Override
    public Result addOrUpdatePcbaCompany(String mode, PcbaCompany pcbaCompany) {
        try {
            if (pcbaCompany == null) {
                return ResultUtil.error(500, "缺少参数pcbaCompany", "Miss Param pcbaCompany", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (pcbaCompany.getPcbaCompanyId() == null || "".equals(pcbaCompany.getPcbaCompanyId().toString())) {
                    return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
                }

                List<PcbaCompany> pcbaCompanies = pcbaCompanyDao.judgeUniquePcbaCompany(pcbaCompany.getPcbaCompanyId(), pcbaCompany.getPcbaCompanyNameCn());
                if (pcbaCompanies.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                PcbaCompany oldPcbaCompany = pcbaCompanyDao.getPcbaCompanyById(pcbaCompany.getPcbaCompanyId());

                pcbaCompany.setUpdateTime(new Date());
                Integer integer = pcbaCompanyDao.updatePcbaCompany(pcbaCompany);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldPcbaCompany), JSONObject.toJSONString(pcbaCompany)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {

                List<PcbaCompany> pcbaCompanyByName = pcbaCompanyDao.getPcbaCompanyByName(pcbaCompany.getPcbaCompanyNameCn());
                if (pcbaCompanyByName.size() > 0) {
                    return ResultUtil.error(500, "该工厂已存在", "Already Exist", null);
                }

                pcbaCompany.setCreateTime(new Date());
                Integer integer = pcbaCompanyDao.addPcbaCompany(pcbaCompany);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(pcbaCompany)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result updateEmailOwners(Integer pcbaCompanyId, String emailOwners) {
        try {
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            if (emailOwners == null || "".equals(emailOwners)) {
                return ResultUtil.error(500, "缺少参数emailOwners", "Miss Param emailOwners", null);
            }
            Integer integer = pcbaCompanyDao.updateEmailOwners(pcbaCompanyId, emailOwners);
            if (integer > 0) {
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result updateEmailOwners2(Integer pcbaCompanyId, String emailOwners2) {
        try {
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            if (emailOwners2 == null || "".equals(emailOwners2)) {
                return ResultUtil.error(500, "缺少参数emailOwners2", "Miss Param emailOwners2", null);
            }
            Integer integer = pcbaCompanyDao.updateEmailOwners2(pcbaCompanyId, emailOwners2);
            if (integer > 0) {
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result updateEmailOwners3(Integer pcbaCompanyId, String emailOwners3) {
        try {
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            if (emailOwners3 == null || "".equals(emailOwners3)) {
                return ResultUtil.error(500, "缺少参数emailOwners3", "Miss Param emailOwners3", null);
            }
            Integer integer = pcbaCompanyDao.updateEmailOwners3(pcbaCompanyId, emailOwners3);
            if (integer > 0) {
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result updateEmailOwners4(Integer pcbaCompanyId, String emailOwners4) {
        try {
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            if (emailOwners4 == null || "".equals(emailOwners4)) {
                return ResultUtil.error(500, "缺少参数emailOwners4", "Miss Param emailOwners4", null);
            }
            Integer integer = pcbaCompanyDao.updateEmailOwners4(pcbaCompanyId, emailOwners4);
            if (integer > 0) {
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result updateEmailOwners5(Integer pcbaCompanyId, String emailOwners5) {
        try {
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            if (emailOwners5 == null || "".equals(emailOwners5)) {
                return ResultUtil.error(500, "缺少参数emailOwners5", "Miss Param emailOwners5", null);
            }
            Integer integer = pcbaCompanyDao.updateEmailOwners5(pcbaCompanyId, emailOwners5);
            if (integer > 0) {
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result updateEmailOwners6(Integer pcbaCompanyId, String emailOwners6) {
        try {
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            if (emailOwners6 == null || "".equals(emailOwners6)) {
                return ResultUtil.error(500, "缺少参数emailOwners6", "Miss Param emailOwners6", null);
            }
            Integer integer = pcbaCompanyDao.updateEmailOwners6(pcbaCompanyId, emailOwners6);
            if (integer > 0) {
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
